namespace Brjnk.GpxExplorer.ViewModel
{
    using Brjnk.Common;
    using Brjnk.GpxExplorer.Data;
    using Brjnk.GpxExplorer.Workspace.Searching;
    using Brjnk.MVVM;

    public class RoutePointVM : Wrapper<RoutePoint>, IGpsData, ISearchResults, IMoveablePoint, ISelectable
    {
        public RoutePointVM(RoutePoint source) : base(source) { }

        public ObservableCollectionEx<SearchResult> SearchResults { get; set; }

        private int number;

        public int Number
        {
            get { return number; }
            set { number = value; OnPropertyChanged("Number"); }
        }

        private Distance distance;

        public Distance Distance
        {
            get { return distance; }
            set { distance = value; OnPropertyChanged("Distance"); }
        }

        private Distance distanceDelta;

        public Distance DistanceDelta
        {
            get { return distanceDelta; }
            set { distanceDelta = value; OnPropertyChanged("DistanceDelta"); }
        }

        private Azimuth? azimuth;

        public Azimuth? Azimuth
        {
            get { return azimuth; }
            set { azimuth = value; OnPropertyChanged("Azimuth"); }
        }

        public GpsCoordinates PositionI
        {
            get
            {
                return Source.Position;
            }
        }

        public string NameI
        {
            get
            {
                var name = string.Format("{0} #{1}", ((RouteVM)this.Parent).Source.Name, this.Number);
                return name;
            }
        }

        public void Move(GpsCoordinates position)
        {
            this.Source.Position = position;
        }

        public void GetBounds(BoundsBuilder builder)
        {
            builder.AddPoint(Source.Position);
        }

        private bool isSelected;

        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; OnPropertyChanged("IsSelected"); }
        }

        protected override void OnSourceObjectPropertyChanged(string propertyName)
        {
            if (propertyName == "Position")
                OnPropertyChanged("PositionI");
        }
    }
}